﻿@page "/clients/{id}/{action}"
@using SimpleIdServer.IdServer.Website.Infrastructures
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.ClientStore;
@using SimpleIdServer.IdServer.Website.Stores.RealmStore;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@inherits Fluxor.Blazor.Web.Components.FluxorLayout
@inject NotificationService notificationService
@inject NavigationManager navigationManager
@inject IUrlHelper urlHelper
@layout MainLayout
@inject IState<ClientState> clientState
@inject IDispatcher dispatcher

<SidAuthorizeView Roles=@("/clients/manage,/clients/view")>
    <Authorized>
        <RadzenBreadCrumb>
            <RadzenBreadCrumbItem Path="@urlHelper.GetUrl("/clients")" Text="@Global.ClientsTitle" />
            <RadzenBreadCrumbItem Text="@Global.ClientDetailsTitle" />
        </RadzenBreadCrumb>

        <RadzenText class="mt-3 mb-3 no-margin" Text="@clientState.Value.Client.ClientId" TextStyle="TextStyle.DisplayH3" />

        <div class="mb-1">
            <RadzenBadge Text="@clientState.Value.Client.ClientType" IsPill="true" />
        </div>

        <RadzenCardLoader Variant="Variant.Filled" IsLoading=@clientState.Value.IsLoading>
            <RadzenTabs Change=@OnChange @bind-SelectedIndex="selectedIndex">
                <Tabs>
                    <RadzenTabsItem Text="@Global.Details">
                        <ClientSettings Client="@clientState.Value.Client"></ClientSettings>
                    </RadzenTabsItem>
                    <RadzenTabsItem Text="@Global.ClientScopes">
                        <ClientScopes Client="@clientState.Value.Client"></ClientScopes>
                    </RadzenTabsItem>
                    <RadzenTabsItem Text="@Global.Keys">
                        <ClientKeys Client="@clientState.Value.Client"></ClientKeys>
                    </RadzenTabsItem>
                    <RadzenTabsItem Text="@Global.Credentials">
                        <ClientCredentials Client="@clientState.Value.Client"></ClientCredentials>
                    </RadzenTabsItem>
                    <RadzenTabsItem Text="@Global.Roles">
                        <ClientRoles Client="@clientState.Value.Client"></ClientRoles>
                    </RadzenTabsItem>
                    <RadzenTabsItem Text="@Global.Advanced">
                        <ClientAdvanced Client="@clientState.Value.Client"></ClientAdvanced>
                    </RadzenTabsItem>
                    <RadzenTabsItem Text="@Global.Realms">
                        <ClientRealms Client="@clientState.Value.Client"></ClientRealms>
                    </RadzenTabsItem>
                </Tabs>
            </RadzenTabs>
        </RadzenCardLoader>
    </Authorized>
    <NotAuthorized>
        <p>@Global.NotAuthorized</p>
    </NotAuthorized>
</SidAuthorizeView>

@code {
    Dictionary<string, int> mappingActionNameToIndex = new Dictionary<string, int>
    {
        { "settings", 0 },
        { "scopes", 1 },
        { "keys", 2 },
        { "credentials", 3 },
        { "roles", 4 },
        { "advanced", 5 },
        { "realms", 6 }
    };

    [Parameter]
    public string id { get; set; } = null!;

    [Parameter]
    public string? action { get; set; } = null;

    int selectedIndex = 0;

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (string.IsNullOrWhiteSpace(id)) return;
        if (!string.IsNullOrWhiteSpace(action) && mappingActionNameToIndex.ContainsKey(action))
            selectedIndex = mappingActionNameToIndex[action];
        SubscribeToAction<GetClientFailureAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = act.ErrorMessage });
            StateHasChanged();
        });
        dispatcher.Dispatch(new GetClientAction { ClientId = id });
    }

    void OnChange(int index)
    {
        var rec = mappingActionNameToIndex.Single(kvp => kvp.Value == index);
        navigationManager.NavigateTo(urlHelper.GetUrl($"/clients/{System.Web.HttpUtility.UrlEncode(id)}/{rec.Key}"));
    }
}